﻿using OpenTK;
using OpenTK.Graphics.OpenGL;

namespace Chimp.Core
{
    public class ChimpParticle : ChimpBase
    {
        private bool ColorDir = true;
        private Vector2 TmpPos;

        public ChimpParticle(string path)
        {
            Visual = new ChimpTexture2D(path);
            Color = new Vector4(0, 0, 0, 0);
            Energy = 0.0f;
            Z = 0.01f;
        }

        public ChimpParticle()
        {
            Color = new Vector4(0, 0, 0, 0);
            Energy = 0.0f;
            Visual = null;
            Z = 0.2f;
        }

        public float ZVel { get; set; }
        public float Energy { get; set; }
        public Vector4 Color { get; set; }
        public ChimpTexture2D Visual { get; set; }
        public float Decline { get; set; }
        public Vector2 I { get; set; }
        public float Z { get; set; }
        public float ColorLow { get; set; }
        public float ColorHigh { get; set; }
        public float ColorMod { get; set; }
        public float ColorSpeed { get; set; }

        public virtual ChimpParticle RequestNew()
        {
            return new ChimpParticle();
        }

        public Vector2 GetPos(Vector2 pos)
        {
            var mid = new Vector2(ChimpApp.W, ChimpApp.H);
            mid = mid*0.5f;

            pos = pos - mid;

            float z = Z;

            pos = ChimpMath.Scale(ref pos, ref z);

            pos = mid + pos;
            return pos;
        }

        public override void OnDraw(Vector2 pos)
        {
            float cv = Prep();

            pos = TmpPos;
            if (pos.X < -128 || pos.X > ChimpApp.W + 128 || pos.Y < -128 || pos.Y > ChimpApp.W + 128)
            {
                return;
            }
            //  Visual.Bind(0);

            float s = cv*16.0f*Scale;
            cv = cv*3.0f;
            cv = cv*ColorMod;
            ChimpDraw.BlendMode = ChimpBlend.Alpha;
            ChimpDraw.Image(new Vector4(pos.X - 16, pos.Y - 16, 32, 32), DrawRotation, cv*DrawScale,
                            new Vector4(cv, cv, cv, cv), Visual, false);

            /*
            GL.Color4(cv, cv, cv, cv);

            GL.Begin(BeginMode.Quads);

            

            pos = DoRect(pos, s);
             
            
            GL.End();

           Visual.Unbind(0);
        */
        }

        public override void OnDrawShape(Vector2 pos)
        {
            float cv = Prep();
            pos = TmpPos;
            ChimpDraw.ShapeBlit(new Vector4(pos.X - 16, pos.Y - 16, 32, 32), DrawRotation, cv*DrawScale, Visual, false);
        }

        private float Prep()
        {
            Vector2 pos = DrawPos;
            float cv = 0.1f + (Z*0.9f);
            ChimpDraw.BlendMode = ChimpBlend.Lighten;
            pos = GetPos(pos);
            TmpPos = pos;
            return cv;
        }

        private static Vector2 DoRect(Vector2 pos, float s)
        {
            GL.TexCoord2(0, 0);
            GL.Vertex2(pos.X - s, pos.Y - s);

            GL.TexCoord2(1, 0);
            GL.Vertex2(pos.X + s, pos.Y - s);

            GL.TexCoord2(1, 1);
            GL.Vertex2(pos.X + s, pos.Y + s);

            GL.TexCoord2(0, 1);
            GL.Vertex2(pos.X - s, pos.Y + s);
            return pos;
        }

        public override bool OnUpdate(Vector2 pos)
        {
            Pos += I;
            Z *= ZVel;

            Energy *= Decline;
            if (Energy < 0.005f)
            {
                Scene.RemoveObj(this);
                Scene = null;
            }
            if (ColorDir)
            {
                ColorMod += ColorSpeed;
                if (ColorMod >= ColorHigh)
                {
                    ColorMod = ColorHigh;
                    ColorDir = false;
                }
            }
            else
            {
                ColorMod -= ColorSpeed;
                if (ColorMod <= ColorLow)
                {
                    ColorMod = ColorLow;
                    ColorDir = true;
                }
            }
            return false;
        }
    }
}